package com.example.ttt.functions;

import com.example.ttt.jcomp.JFrameComponent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.util.List;
import java.util.Objects;

/*
* 向表格填充列表数据
* */
@Component
@Slf4j
public class TableFill {

    @Autowired
    private JFrameComponent jFrameComponent;

    public void tableFillData(List lists) {

        String[][] rowData = jFrameComponent.getRowData();
        JTable table11 = jFrameComponent.getTable11();

        if (Objects.nonNull(lists)){
            rowData = new String[lists.size()][1];
            //向列表填充数据
            for (int i = 0; i < lists.size(); i++) {
                for (int j = 0; j < rowData[i].length; j++) {
                    rowData[i][j] = String.valueOf("  " + lists.get(i));
                }
            }
            Object[] columnNames = {"A"};
            DefaultTableModel model = new DefaultTableModel(rowData, columnNames) {
                @Override
                public boolean isCellEditable(int row, int column) {
                    // TODO 自动生成的方法存根
                    return false;
                }
            };
            table11.setModel(model);
            log.info("更新数据中……");
        }else {
            log.info("更新数据失败，地址为空……");
        }
    }
}
